package com.fhj.bookshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fhj.bookshop.common.R;
import com.fhj.bookshop.entity.Favourite;
import com.fhj.bookshop.entity.Product;
import com.fhj.bookshop.service.IFavouriteService;
import com.fhj.bookshop.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fu
 * @since 2023-12-24
 */
@RestController
public class FavouriteController {

    @Autowired
    IFavouriteService favouriteService;
    @Autowired
    IProductService productService;

    @RequestMapping(value="/favourite/{custId}/{proId}",method = RequestMethod.POST)
    public R addFav(@PathVariable Integer custId,@PathVariable Integer proId){
        Product product=productService.findProductById(proId);
        Favourite favourite=new Favourite(null,custId,proId,product.getProName(),product.getProImg(),product.getProPrice(),"");
        favouriteService.addFavourite(favourite);
        return R.ok("插入成功").setData(favourite);
    }

    @RequestMapping(value = "/removeFavourite/{custId}/{proId}",method = RequestMethod.GET)
    public R removeFav(@PathVariable Integer custId,@PathVariable Integer proId){
        favouriteService.removeFavourite(custId,proId);
        return R.ok("删除成功");
    }

    @RequestMapping(value ="/checkFav/{custId}/{proId}",method =RequestMethod.GET)
    public R checkFav(@PathVariable Integer custId,@PathVariable Integer proId){
        Integer result=favouriteService.checkFavourite(custId,proId);
        return R.ok("检查完毕").setData(result);
    }

    @RequestMapping(value = "/displayFav/{custId}",method = RequestMethod.GET)
    public R displayFav(@PathVariable Integer custId){
        return R.ok("展示成功").setData(favouriteService.findByCustId(custId));
    }
}
